/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * jifRerporteLicitacion.java
 *
 * Created on 25-ago-2010, 15:59:55
 */

package Licitaciones;

import administracion.delegate.GestionPedidosDelegate;
import administracion.helper.Listas;
import administracion.helper.Utiles_MensajesDialogo;
import java.awt.event.KeyEvent;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.view.JasperViewer;
import suministros.ejb30.TO.EmpresaTO;
import suministros.ejb30.TO.TipoTO;

/**
 *
 * @author Diana
 */
public class jifRerporteLicitacion extends javax.swing.JInternalFrame {
      List<TipoTO> listaTipos = null;
      List<EmpresaTO> empresas;
    /** Creates new form jifRerporteLicitacion */
    public jifRerporteLicitacion() {
        initComponents();
        empresas = Listas.getEmpresas();
        if(empresas.isEmpty()){
            this.jCheckBox1.setEnabled(false);
        }
        this.jCheckBox1.setSelected(false);
        this.llenarComboEmpresa();
        llenarComboTipo();
    }
       private void llenarComboTipo() {
        List listaTipos_cbo1 = this.cboBeanBindingTipo1.getLista();
        listaTipos_cbo1.clear();
        try
        {
          listaTipos = Listas.getTipo_porTabla("tipo_licitacion");
          this.cboBeanBindingTipo1.setLista(listaTipos);
          this.bindingGroup.bind();
          this.jcboTipo.updateUI();
          this.jcboTipo.setSelectedIndex(-1);
        } catch (Exception ex) {
          Utiles_MensajesDialogo.mensajeAlerta("Error al obtener tipos\n" + ex.getMessage());
          ex.printStackTrace();
          Logger.getLogger(jifRerporteLicitacion.class.getName()).log(Level.SEVERE, null, ex);
        }
          this.jcbEmpresa.setEnabled(false);
      }
       public void limpiar(){
           this.jcboTipo.setSelectedIndex(-1);
           this.jtfDias.setText("");
           if(this.jchkInversion.isSelected()){
               this.jcboTipo.setEnabled(false);
               this.jtfDias.setEnabled(false);
           }
           else{
               this.jcboTipo.setEnabled(true);
               this.jtfDias.setEnabled(true);
           }

       }
      public  void cerrar(){
      int res = javax.swing.JOptionPane.showConfirmDialog( this,"Esta seguro de salir?",
      "Confirmar",javax.swing.JOptionPane.YES_NO_OPTION );
            if(res==javax.swing.JOptionPane.YES_OPTION){
                    this.dispose();

         }
      }
       private void llenarComboEmpresa() {
        empresas = null;
        List listaPaises_cbo = this.cboBeanBindingEmpresa1.getLista();
        listaPaises_cbo.clear();
        try
        {
          if(this.jCheckBox1.isSelected()){
                empresas = Listas.getEmpresas();
            }
            else{
                empresas = Listas.getEmpresaPrincipal();
            }
          this.cboBeanBindingEmpresa1.setLista(empresas);
          this.bindingGroup.bind();
          this.jcbEmpresa.updateUI();
          this.jcbEmpresa.setSelectedIndex(0);
        } catch (Exception ex) {
          Utiles_MensajesDialogo.mensajeAlerta("Error al obtener empresas\n" + ex.getMessage());
          ex.printStackTrace();
          Logger.getLogger(jifRerporteLicitacion.class.getName()).log(Level.SEVERE, null, ex);
        }
      }
    public void GenerarReporte(){
        try {
        JasperViewer jv = null;
        JasperPrint jp = null;
        Map parametros = new HashMap();
        String emp=empresas.get(this.jcbEmpresa.getSelectedIndex()).getCodigo();
        if(this.jchkInversion.isSelected()){
            parametros.put("id_empresa", emp);
            jp=GestionPedidosDelegate.getInstance().generarReporteIngreso("ReporteLicitacion.jasper", parametros);
        }
        if(!this.jchkInversion.isSelected()){
            if(this.jcboTipo.getSelectedIndex()>-1){
                if(this.jtfDias.getText().length()>0){
                    parametros.put("dias",Integer.parseInt(this.jtfDias.getText()));
                    parametros.put("idTipo", (listaTipos.get(this.jcboTipo.getSelectedIndex())).getId_tabla_tipo());
                    parametros.put("id_empresa", emp);
                    jp=GestionPedidosDelegate.getInstance().generarReporteIngreso("ReporteLicitacionTodosTipo.jasper", parametros);

                }
                else{
                    parametros.put("idTipo", (listaTipos.get(this.jcboTipo.getSelectedIndex())).getId_tabla_tipo());
                    parametros.put("id_empresa", emp);
                    jp=GestionPedidosDelegate.getInstance().generarReporteIngreso("ReporteLicitacionTipo.jasper", parametros);

                }
            }
            else{
                if(this.jtfDias.getText().length()>0){
                    parametros.put("dias",Integer.parseInt(this.jtfDias.getText()));
                    parametros.put("id_empresa", emp);
                    jp=GestionPedidosDelegate.getInstance().generarReporteIngreso("ReporteLicitacionTodos.jasper", parametros);

                }
                else{
                    parametros.put("id_empresa", emp);
                    jp=GestionPedidosDelegate.getInstance().generarReporteIngreso("ReporteLicitacion.jasper", parametros);

                }

            }

        }
      
            jv = new JasperViewer(jp, false);
            jv.setExtendedState(javax.swing.JFrame.MAXIMIZED_BOTH);
            jv.setVisible(true);
            parametros.clear();
        }catch (Exception e) {
            Utiles_MensajesDialogo.mensajeError("No se pudo realizar la transaccion");
        }


  }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        cboBeanBindingTipo1 = new administracion.BeanBinding.CboBeanBindingTipo();
        cboBeanBindingEmpresa1 = new administracion.BeanBinding.CboBeanBindingEmpresa();
        jPanel1 = new javax.swing.JPanel();
        jtfDias = new javax.swing.JTextField();
        jchkInversion = new javax.swing.JCheckBox();
        jcbEmpresa = new javax.swing.JComboBox();
        jCheckBox1 = new javax.swing.JCheckBox();
        jLabel1 = new javax.swing.JLabel();
        jcboTipo = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jbtCancelar = new javax.swing.JButton();
        jbtBuscar = new javax.swing.JButton();

        setIconifiable(true);
        setTitle("REPORTE LICITACION");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Busqueda Avanzada", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 255))); // NOI18N

        jtfDias.setFont(new java.awt.Font("Arial", 0, 12)); // NOI18N

        jchkInversion.setFont(new java.awt.Font("Arial", 1, 12));
        jchkInversion.setText("LICITACIONES A VENCER POR DEFECTO");
        jchkInversion.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jchkInversionActionPerformed(evt);
            }
        });

        org.jdesktop.beansbinding.ELProperty eLProperty = org.jdesktop.beansbinding.ELProperty.create("${lista}");
        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, cboBeanBindingEmpresa1, eLProperty, jcbEmpresa);
        bindingGroup.addBinding(jComboBoxBinding);

        jcbEmpresa.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jcbEmpresaKeyPressed(evt);
            }
        });

        jCheckBox1.setFont(new java.awt.Font("Arial", 1, 12)); // NOI18N
        jCheckBox1.setText("OTRAS EMPRESAS");
        jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox1ActionPerformed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel1.setText("TIPO LICITACION:");

        eLProperty = org.jdesktop.beansbinding.ELProperty.create("${lista}");
        jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, cboBeanBindingTipo1, eLProperty, jcboTipo);
        bindingGroup.addBinding(jComboBoxBinding);

        jcboTipo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jcboTipoKeyPressed(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel2.setText("NUMERO DIAS:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(9, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(11, 11, 11)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(29, 29, 29)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtfDias, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jcboTipo, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(jCheckBox1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jcbEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jchkInversion))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jcbEmpresa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCheckBox1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jchkInversion)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jcboTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtfDias, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jbtCancelar.setFont(new java.awt.Font("Arial", 1, 12));
        jbtCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/button_cancel.png"))); // NOI18N
        jbtCancelar.setText("CERRAR");
        jbtCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtCancelarActionPerformed(evt);
            }
        });

        jbtBuscar.setFont(new java.awt.Font("Arial", 1, 12));
        jbtBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/iconoscomb/16x16ozygen/actions/find.png"))); // NOI18N
        jbtBuscar.setText("BUSCAR");
        jbtBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtBuscarActionPerformed(evt);
            }
        });
        jbtBuscar.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jbtBuscarFocusGained(evt);
            }
        });
        jbtBuscar.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jbtBuscarKeyPressed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(91, 91, 91)
                .addComponent(jbtBuscar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtCancelar))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtBuscar)
                    .addComponent(jbtCancelar))
                .addContainerGap(27, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jcboTipoKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jcboTipoKeyPressed
        if(KeyEvent.VK_ENTER == evt.getKeyCode()){
            this.jtfDias.requestFocus();
        }
}//GEN-LAST:event_jcboTipoKeyPressed

    private void jbtBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtBuscarActionPerformed
    if(this.jcbEmpresa.getSelectedIndex()>=0){
        this.GenerarReporte();
    }
    else{
        Utiles_MensajesDialogo.mensajeError("No se ha seleccionado ninguna empresa");
    }
            

}//GEN-LAST:event_jbtBuscarActionPerformed

    private void jbtBuscarFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jbtBuscarFocusGained

}//GEN-LAST:event_jbtBuscarFocusGained

    private void jbtBuscarKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jbtBuscarKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER ){
            this.jbtBuscar.doClick();
        }
}//GEN-LAST:event_jbtBuscarKeyPressed

    private void jbtCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtCancelarActionPerformed
        cerrar();
}//GEN-LAST:event_jbtCancelarActionPerformed

    private void jchkInversionActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jchkInversionActionPerformed

        this.limpiar();
}//GEN-LAST:event_jchkInversionActionPerformed

    private void jcbEmpresaKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jcbEmpresaKeyPressed
    if (evt.getKeyCode() == KeyEvent.VK_ENTER ){
            if(this.jcbEmpresa.getSelectedIndex()>=0){
                this.jcboTipo.requestFocus();
            }
        }
}//GEN-LAST:event_jcbEmpresaKeyPressed

    private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
        if(this.jCheckBox1.isSelected()){
            this.jcbEmpresa.setEnabled(true);
        } else{
            this.jcbEmpresa.setEnabled(false);
        }
        this.llenarComboEmpresa();
     
}//GEN-LAST:event_jCheckBox1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private administracion.BeanBinding.CboBeanBindingEmpresa cboBeanBindingEmpresa1;
    private administracion.BeanBinding.CboBeanBindingTipo cboBeanBindingTipo1;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JButton jbtBuscar;
    private javax.swing.JButton jbtCancelar;
    private javax.swing.JComboBox jcbEmpresa;
    private javax.swing.JComboBox jcboTipo;
    private javax.swing.JCheckBox jchkInversion;
    private javax.swing.JTextField jtfDias;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

}
